//
//  AudioViewController.m
//  PhoenixNews
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AudioViewController.h"
#import "PhoneixNews.pch"
#import "AudioModel.h"
#import "AudioTableViewCell.h"
#import "AudioCollectionViewCell.h"
#import "VideoTypeViewController.h"
#import "VideoViewController.h"
@interface AudioViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mutableArray;
@property (nonatomic, retain) NSMutableArray *ViedeoArray;
@property (nonatomic, retain) NSMutableArray *QPmutableArray;

@end

@implementation AudioViewController

- (void)dealloc {
    [_tableView release];
    [_mutableArray release];
    [_ViedeoArray release];
    [_QPmutableArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 视频Cell
    [_tableView registerClass:[AudioTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView release];
    [self getData];
    
}

#pragma mark - 解数剧
- (void)getData {
      AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:Audio parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        // 四个图片数组
        NSArray *array = dic[@"videoSidList"];
        self.mutableArray = [AudioModel modelHanderWithArray:array];
        ;
        // 页面视频
        NSArray *arrayTwo = dic[@"videoList"];
        self.ViedeoArray = [AudioModel modelHanderWithArray:arrayTwo];
       
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
   
    
}


#pragma mark -TableView协议方法




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _ViedeoArray.count;
}
#pragma mark - Cell 重复引用问题解决方法



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AudioModel *audio = _ViedeoArray[indexPath.row];
  
   
    AudioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
          
        [cell.VideoImage sd_setImageWithURL:[NSURL URLWithString:audio.cover]];
        [cell.buttonImage setImage:[UIImage imageNamed:@"buttonImage"] forState: UIControlStateNormal];
     return cell;
       
}
   

    

// cell点击时间
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AudioModel *audio = _ViedeoArray[indexPath.row];
    VideoViewController *video = [[VideoViewController alloc] init];
    video.urlStr = audio.mp4_url;
   [self.navigationController pushViewController:video animated:YES];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 400;
}
// 头试图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 180;
}
#pragma mark -UICollectionView创建
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 180)];
    headView.backgroundColor = [UIColor redColor];
    UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc] init];
    layou.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layou.itemSize = CGSizeMake(70, 900/ 4);
    layou.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 20, self.view.frame.size.width - 30, 140) collectionViewLayout:layou];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[AudioCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [headView addSubview: collectionView];
    
    return headView;
    
    
}
#pragma mark -UICollection协议方法
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mutableArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AudioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    AudioModel *audio = _mutableArray[indexPath.row];
    [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:audio.imgsrc]];
    cell.title.text = audio.title;
    
    return cell;
}
#pragma mark -UICellection点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    VideoTypeViewController *video = [[VideoTypeViewController alloc] init];
    
// 传值到下一页面
    if (indexPath.item == 0) {
        
        NSString *QPstr =@"http://c.3g.163.com/nc/video/list/VAP4BFE3U/n/10-10.html";
         video.strQP = QPstr;
    }else if(indexPath.item == 1) {
            NSString *MWstr = @"http://c.3g.163.com/nc/video/list/VAP4BFR16/n/0-10.html";
            video.strMW = MWstr;
    }else if (indexPath.item == 2) {
        NSString *girl = @"http://c.3g.163.com/nc/video/list/VAP4BG6DL/n/0-10.html";
        video.strGirl = girl;
    }else if (indexPath.item == 3) {
        NSString *boutique = @"http://c.3g.163.com/nc/video/list/VAP4BGTVD/n/0-10.html";
        video.strBoutique = boutique;
    }
   

    
    [self.navigationController pushViewController:video animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
